package cn.bulgat.spring.web.config;

import cn.bulgat.common.id.IIdGenerator;
import cn.bulgat.spring.web.filter.LogIdFilter;
import org.springframework.boot.web.servlet.FilterRegistrationBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.Ordered;

import javax.annotation.Resource;

@Configuration
public class FilterConfig {

    @Resource
    private IIdGenerator<Long> idGenerator;

    @Bean
    public FilterRegistrationBean<LogIdFilter> logIdFilter() {
        FilterRegistrationBean<LogIdFilter> registrationBean = new FilterRegistrationBean<>();
        registrationBean.setFilter(new LogIdFilter(idGenerator));
        registrationBean.addUrlPatterns("/*");
        registrationBean.setOrder(Ordered.HIGHEST_PRECEDENCE);
        return registrationBean;
    }
}